package com.streetsmart.main;

/**
 * Used to manage the eevents/tasks
 */
import java.util.Calendar;
import java.util.Date;

import com.streetsmart.DBManager;
import com.streetsmart.R;
import com.streetsmart.Utilities;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
// TODO add features to view, update, delete events. For example on clicking the event create an activity that will give options to edit or delete the event
public class SSTaskOverviewActivity extends ListActivity implements
		OnItemSelectedListener {
	Spinner spinner;
	ListView listview;
	Cursor cur;
	Context context = this;
	DBManager dbhelper;
	private int selection = 6;
	SimpleCursorAdapter scadapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		// TODO add a customized view to the list displayed each time. something
		// like on clicking display further details about the task/event.
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.sstask_overview);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.sswindow_title);
		dbhelper = new DBManager(this);
		spinner = (Spinner) findViewById(R.id.spinnerOverviewType);
		spinner.setOnItemSelectedListener(this);
		ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter
				.createFromResource(context, R.array.TaskOverviews,
						android.R.layout.simple_spinner_item);
		arrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(arrayAdapter);
		spinner.setSelection(selection);
		listview =getListView();
		//mlistview = (ListView) findViewById(R.id.listViewOverViewList);
	}

	@Override
	public void onResume() {
		super.onResume();
		if(cur != null) cur.close();
		cur = dbhelper.getAllEvents();
		fetchData(cur);
		spinner.setSelection(selection);
	}
	
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO get databse query, return cursor and build the list
		String option = arg0.getItemAtPosition(arg2).toString();
		if(cur != null) { cur.close(); }
		if (option.equals("Tasks completed")) {
			selection = 0;
			cur = dbhelper.getAllCompletedEvents();
		} else if (option.equals("Tasks due today")) {
			selection = 1;
			cur = dbhelper.getEventsDueToday();
		} else if (option.equals("Tasks due tomorrow")) {
			selection = 2;
			cur = dbhelper.getEventsDueTomorrow();
		} else if (option.equals("Tasks due this week")) {
			selection = 3;
			cur = dbhelper.getEventsDueThisWeek();
		} else if (option.equals("Tasks due next week")) {
			selection = 4;
			cur = dbhelper.getEventsDueNextWeek();
		} else if (option.equals("Overdue tasks")) {
			selection = 5;
			cur = dbhelper.getOverdueEvents();
		} else if (option.equals("All tasks")) {
			selection = 6;
			cur = dbhelper.getAllEvents();
		} else if (option.equals("Must to do tasks")) {
			selection = 7;
			cur = dbhelper.getAllEvents("Must");
		} else if (option.equals("Good to do tasks")) {
			selection = 8;
			cur = dbhelper.getAllEvents("Good");
		}
		fetchData(cur);
	}
	
	public void fetchData(Cursor c){
		startManagingCursor(c);
		String[] from = new String[] { "name" };
		int[] to = new int[] { R.id.listItem };
		scadapter = new SimpleCursorAdapter(this,
				R.layout.testdbrow, cur, from, to);
		scadapter.notifyDataSetChanged();
		listview.setAdapter(scadapter);
		listview = getListView();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
		c.moveToPosition(position);
		Intent i = new Intent(context, SSManageTaskActivity.class);
		i.putExtra("id", Long.parseLong(String.valueOf(c.getInt(0))));
		i.putExtra("name", c.getString(1));
		i.putExtra("category", findCategoryIndex(c.getString(2)));
		String temp = c.getString(5);
		if(temp.equals("autopick")){
			i.putExtra("where", "autopick");
		}
		else{
			i.putExtra("where", "specific");
			i.putExtra("location", temp);
		}
		temp = c.getString(9);
		if(temp.equals("Good")){
			i.putExtra("when","Good");
		}else{
			i.putExtra("when", "Must");
		}
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(c.getLong(6));
		temp = "" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR); 
		i.putExtra("startdate", temp);
		temp = "" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);
		i.putExtra("starttime", temp);
		
		cal.setTimeInMillis(c.getLong(7));
		temp = "" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR); 
		i.putExtra("enddate", temp);
		temp = "" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);
		i.putExtra("endtime", temp);
		stopManagingCursor(c);
		dbhelper.close();
		startActivity(i);
	}
	
	public int findCategoryIndex(String value) {
		String[] categories = getResources().getStringArray(R.array.categories);
		int length = categories.length;
		for(int i = 0; i < length; i++){
			if(value.equals(categories[i])){
				return i;
			}
		}
		return -1;
	}
	
	public void onNothingSelected(AdapterView<?> arg0) {
	}
	
	

	public void onBackPressed() {
		cur.close();
		dbhelper.close();
		finish();
	}

	public void onDestroy() {
		cur.close();
		dbhelper.close();
		super.onDestroy();
	}
}
